$(function () {
    getAllInDepartment();
});

/**
 * 获取所有内部部门信息
 */
function getAllInDepartment() {
    $("#inDepartment").bootstrapTable({
        url: '/super/getAllInDepartment',
        contentType: 'application/x-www-form-urlencoded',
        method: 'POST',
        toolbar: '#toolbar',
        sidePagination: 'server',
        pageNumber: 1,//当前页
        pageSize: 15,//每页条数
        pageList: [15, 50, 100, 'ALL'],
        dataType: 'json',//返回数据的类型
        /*detailView: true,*/
        cache: false,
        pagination: false,//使用分页
        /*showRefresh: true,//右上角刷新按钮
        showColumns: true,//右上角,列,列表*/
        /*queryParams: function (params) {
            params["parentId"] = $("#parent-select").val();
            return params;
        },*/
        classes: 'table table-hover table-striped',
        columns: [{
            field: "id",
            title: "编号",
            align: "center"
        }, {
            field: "name",
            title: "部门名称",
            align: "center"
        }, {
            field: "descriptor",
            title: "描述",
            align: "center"
        }, {
            field: "operator",
            title: "操作",
            align: "center",
            formatter: function (value, row, index) {
                var str = "<a href='javascript:void(0);' onclick='javascript:openChangeInDepartmentModal(" + row.id + ");'>修改  </a>" +
                    "<a href='javascript:void(0);' onclick='javascript:delInDepartment(" + row.id + ")'>删除</a>";
                return str;
            }
        }]
    });
}

/**
 * 打开添加内部部门模态框
 */
function openAddInDepartmentModal() {
    $("#addModal").modal("show");
    $("#addModal .modal-body").load("/super/openAddInDepartment.html");

}

/**
 * 打开修改模态框并显示部门信息
 * @param inDepartmentId 内部部门编号
 */
function openChangeInDepartmentModal(inDepartmentId) {
    $("#changeModal").modal("show");
    $("#changeModal .modal-body").load("/super/openChangeInDepartment.html", {"inDepartmentId": inDepartmentId});
}

/**
 * 提交修改的内部部门信息
 */
function submitChangeInDepartment() {

    $.post(
        "/super/submitChangeInDepartment",
        $("form").serialize(),
        function (data) {
            if (data.updates == 1) {
                swal("修改成功!", "您已成功修改了一个内部部门信息!", "success");
                refresh();
                $("#changeModal").modal("hide");
            } else {
                swal("修改失败!", "您的操作未成功!", "error");
                $("#changeModal").modal("hide");
                refresh();
            }
        }, "json");
}

/**
 * 根据编号删除一条部门信息
 * @param inDepartmentId 将要删除的部门编号
 */
function delInDepartment(inDepartmentId) {
    swal({
            title: "您确认要删除这个部门吗？",
            text: "删除之后不可恢复，在删除之前应确保该部门已无员工!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定删除！",
            cancelButtonText: "取消删除！",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function (isConfirm) {
            if (isConfirm) {
                $.post(
                    "/super/delInDepartment",
                    {
                        "inDepartmentId": inDepartmentId
                    }, function (data) {
                        if (data.updates == 1) {
                            swal("删除成功！", "该部门已删除!",
                                "success");
                            $("#inDepartment").bootstrapTable("refresh");
                        } else {
                            swal("删除失败！", "可能是服务器忙或该部门下还存在员工",
                                "error");
                            $("#inDepartment").bootstrapTable("refresh");
                        }
                    }, "json"
                );
            } else {
                swal("删除失败！", "您取消了操作!","error");
            }
        }
    );
}

/**
 * 表格刷新
 */
function refresh() {
    getAllInDepartment();
}

function submitAddInDepartment() {
    $.post(
        "/super/submitAddInDepartment",
        $("form").serialize(),
        function (data) {
            if (data.updates == 1) {
                refresh();
                swal("添加成功!", "您已成功添加了一个内部部门!", "success");
            } else {
                swal("添加失败!", "您的操作未成功!", "error");
                getAllInDepartment();
            }
        }, "json");
}